package com.airlines.system.controller;


import com.airlines.system.domain.SysUser;
import com.airlines.system.service.SysTypeService;
import com.airlines.system.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 权限管理模块-系统用户控制器
 */
@Controller
@RequestMapping(value = "/api/system/user")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysTypeService sysTypeService;

    /**
     * 进入用户授权页面
     */
    @RequestMapping
    public String toPage(Model model) {
        model.addAttribute("types", sysTypeService.findAll());
        return "system/accessManagement/userAccess";
    }

    /**
     * 用户授权页面初始化数据
     */
    @RequestMapping(value = "list", method = RequestMethod.GET)
    @ResponseBody
    public List<SysUser> findAllUser(@RequestParam(required = false) String workNo, @RequestParam(required = false) String userName) {
        List<SysUser> users = sysUserService.findAllUser(workNo, userName);
        return users;
    }

    /**
     * 进入用户的角色授权页面
     */
    @RequestMapping(value = "addRolePage/{userId}")
    public String toEditRolePage(@PathVariable String userId, Model model) {
        SysUser user = sysUserService.findAllRole(userId);
        model.addAttribute("user", user);
        return "system/accessManagement/userAddRole";
    }

    /**
     * 进入查看当前用户拥有的角色权限列表
     */
    @RequestMapping(value = "viewRole/{userId}")
    public String viewRole(@PathVariable String userId, Model model) {
        SysUser user = sysUserService.findOwnRole(userId);
        model.addAttribute("user", user);
        return "system/accessManagement/userViewRole";
    }

    /*@RequestMapping(value = "/{userId}", method = RequestMethod.DELETE)
    public ApiResponse<Void> deleteOneUser(@PathVariable String userId) {
        ApiResponse<Void> apiResponse = new ApiResponse<>();
        sysUserService.deleteUser(userId);
        return apiResponse;
    }*/

    /*@ApiOperation("为用户增加角色")
    @RequestMapping(value = "/{userId}/role/{roleId}", method = RequestMethod.GET)
    public ApiResponse<List<SysRole>> addRole2User(@PathVariable Integer userId, @PathVariable String roleId) {
        ApiResponse<List<SysRole>> apiResponse = new ApiResponse<>();

        sysUserService.addRole2User(userId, roleId);

        SysUser sysUserInfo = sysUserService.findOneUserWithRoles(userId);
        apiResponse.setData(sysUserInfo.getSysRoles());
        return apiResponse;
    }*/

   /* @ApiOperation("修改密码")
    @RequestMapping(value = "/{userId}/paswd", method = RequestMethod.GET)
    public ApiResponse<Void> changePassword(@PathVariable Integer userId, @RequestParam String newp) {
        ApiResponse<Void> apiResponse = new ApiResponse<>();

        sysUserService.changePassword(userId,newp);
        return apiResponse;
    }*/

}
